package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import com.atguigu.gmall.service.RabbitService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author mgx
 * @create 2024-09-03 21:20
 * @Description
 */
@RestController
@RequestMapping("/dead/mq")
public class DeadLetterController {
    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendDelay")
    public Result sendDelay() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("消息发送时间:" + sdf.format(new Date()));
        rabbitService.sendDelayMessage(DelayedMqConfig.exchange_delay, DelayedMqConfig.routing_delay,
                         "我是延迟插件的消息", 10);
        // rabbitTemplate.convertAndSend(DelayedMqConfig.exchange_delay, DelayedMqConfig.routing_delay,
        //         "我是延迟插件的消息", new MessagePostProcessor() {
        //             @Override
        //             public Message postProcessMessage(Message message) throws AmqpException {
        //
        //                 message.getMessageProperties().setDelay(10 * 1000);
        //                 System.out.println("消息发送时间:" + sdf.format(new Date()));
        //                 return message;
        //             }
        //         });
        return Result.ok();
    }


    /**
     * 测试发送死信消息-通过死信队列
     * @return
     */
    @GetMapping("/sendDead")
    public Result sendDead() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rabbitService.sendMessage("exchange.dead", "routing.dead.1", "你好，我是消息");
        System.out.println(simpleDateFormat.format(new Date()));
        return Result.ok();
    }
}
